package com.example.tao.mycar.login;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.activity.HomeActivity;

public class LoginActivity extends MyActionBarActivity implements View.OnClickListener {

    private EditText phoneText;
    private EditText passwordText;
    private Button loginButton, registerButton, findPasswordButton;
    private ImageView deletePassword, deleteUser, weixinLogin, weiboLogin, qqLogin;
    private String phoneNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_login);
        setToolBar(getResources().getString(R.string.login), false);

        initView();

        phoneText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                phoneNumber = phoneText.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    loginButton.setEnabled(false);
                } else {
                    loginButton.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initView(){
        phoneText = (EditText) this.findViewById(R.id.inputNum);
        passwordText = (EditText)this.findViewById(R.id.password);

        loginButton = (Button)this.findViewById(R.id.login);
        registerButton = (Button)this.findViewById(R.id.register);
        findPasswordButton = (Button)this.findViewById(R.id.find_password);

        deleteUser = (ImageView)this.findViewById(R.id.deleteUser);
        deletePassword = (ImageView)this.findViewById(R.id.deletePassword);

        weixinLogin = (ImageView)this.findViewById(R.id.wx_login);
        weiboLogin = (ImageView)this.findViewById(R.id.wb_login);
        qqLogin = (ImageView)this.findViewById(R.id.qq_login);

        loginButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
        findPasswordButton.setOnClickListener(this);
        deleteUser.setOnClickListener(this);
        deletePassword.setOnClickListener(this);
        weixinLogin.setOnClickListener(this);
        weiboLogin.setOnClickListener(this);
        qqLogin.setOnClickListener(this);
        if (MyApplication.DEBUG = false) {
            loginButton.setEnabled(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login :
                login();
                break;
            case R.id.register :
                Intent intent = new Intent(LoginActivity.this, PhoneVerifyActivity.class);
                startActivity(intent);
                break;
            case R.id.find_password :
                Intent findPasswordIntent = new Intent(LoginActivity.this, FindPasswordActivity.class);
                startActivity(findPasswordIntent);
                break;
            default:
                break;
        }
    }

    private void login(){
        if (MyApplication.DEBUG = true) {
            startActivity(new Intent(LoginActivity.this, HomeActivity.class));
        }

        String password = passwordText.getText().toString();
        if (!TextUtils.isEmpty(phoneNumber) && !TextUtils.isEmpty(password)) {
            AVUser.logInInBackground(phoneNumber, password, new LogInCallback<AVUser>() {
                @Override
                public void done(AVUser avUser, AVException e) {

                    if (e == null) {
                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(intent);
                    } else {
                        switch (e.getCode()) {
                            case 210 :
                                Toast.makeText(LoginActivity.this, "请输入正确的用户名和密码！", Toast.LENGTH_LONG).show();
                                break;
                            case 211 :
                                Toast.makeText(LoginActivity.this, "用户名不可用！", Toast.LENGTH_LONG).show();
                                break;
                        }
                    }
                    if (e.getMessage().equals("java.net.UnknownHostException")){
                        Toast.makeText(LoginActivity.this, "请连接网络后再登录！", Toast.LENGTH_LONG).show();
                    }
                    e.printStackTrace();
                }
            });
        } else {
            Toast.makeText(LoginActivity.this, "用户名或密码不能为空！", Toast.LENGTH_LONG).show();
        }
    }
}
